/**
 * 数组降维
 * @param {Array} list
 * @returns 降维后的数组 [[1],[2]] => [1,2]
 */
export const flattenDeep = (list: Array<any>): Array<any> => {
  return list.reduce((acc: Array<any>, val: Array<any>) => Array.isArray(val) ? acc.concat(flattenDeep(val)) : acc.concat(val), []);
}

/**
 * @param copy 深拷贝
 * @param _object 如果不传返回为空对象 必须是js的{}对象
 * @param _obj 可选 返回传入的@param _object 必须是js的{}对象,
 */
type _typeObj = { [anyKey: string]: any }
export const copy = (_object: _typeObj, _obj: _typeObj = {}): _typeObj => {
  if (!(Object.prototype === Object.getPrototypeOf(_object))) {
    return new Error('传入参数***_object***类型错误')
  }
  for (let key in _object) {
    if (Object.prototype === Object.getPrototypeOf(_object[key])) {
      _obj[key] = copy(_object[key])
    } else {
      _obj[key] = _object[key]
    }
  }
  return _obj
}

/**
 *
 * @param filename 文件名
 * @returns 返回扩展名
 */
export const extName = (filename: string): string | boolean => {
  if (!filename || typeof filename != 'string') {
    return false
  }
  let a = filename
    .split('')
    .reverse()
    .join('')
  let b = a
    .substring(0, a.search(/\./))
    .split('')
    .reverse()
    .join('')
  return b
}


// 特殊纵向合并
/**
 * @param {*} arr 需要处理的数组列
 * @param {*} rowIndex  行序号
 * @param {*} columnIndex  列序号
 * @param {*} tableData  表格数据
 * @param {*} key 主键key名
 * @returns
 */
export const verticalMerge = (arr: Array<number> = [], rowIndex: number, columnIndex: number, tableData: Array<any>, key: string) => {

  if (tableData.length < 2) {
    return [1, 1]
  }
  if (arr.includes(columnIndex)) {
    let num = 0
    if (rowIndex == 0) {
      if (tableData[rowIndex][key] == tableData[rowIndex + 1][key]) {
        for (let i = rowIndex; i <= tableData.length - 1; i++) {
          if (tableData[i][key] != tableData[rowIndex + 1][key]) {
            break
          } else {
            num++
          }
        }
        if (num === 0) {
          num++
        }
        return [num, 1]
      }
    } else if (tableData[rowIndex][key] != tableData[rowIndex - 1][key]) {
      for (let i = rowIndex; i <= tableData.length - 1; i++) {
        num++
        if (i != tableData.length - 1 && tableData[i][key] != tableData[i + 1][key]) {
          break
        }
      }
      return [num, 1]
    } else {
      return [0, 0]
    }
  }
}

/**
 * @param getDaysBetween 取两个日期之间的差(天数)
 * @param {string} dateString1 日期1 2021-12-20
 * @param {string} dateString2 日期2 2022-01-24
 * @returns {string} 日期之间的差值
 */
export const getDaysBetween = (dateString1: string, dateString2: string): number => {
  var startDate = Date.parse(dateString1)
  var endDate = Date.parse(dateString2)
  if (startDate > endDate) {
    return 0
  }
  if (startDate == endDate) {
    return 1
  }
  var days = (endDate - startDate) / (1 * 24 * 60 * 60 * 1000)
  return days
}

//完成将 toChineseNum， 可以将数字转换成中文大写的表示，处理到万级别，例如 toChineseNum(12345)，返回 一万二千三百四十五。
export const toChinesNum = (num: any): string => {
  let changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']; //changeNum[0] = "零"
  let unit = ["", "十", "百", "千", "万"];
  num = parseInt(num);
  let getWan = (temp: any) => {
    let strArr = temp.toString().split("").reverse();
    let newNum = "";
    for (var i = 0; i < strArr.length; i++) {
      newNum = (i == 0 && strArr[i] == 0 ? "" : (i > 0 && strArr[i] == 0 && strArr[i - 1] == 0 ? "" : changeNum[strArr[i]] + (strArr[i] == 0 ? unit[0] : unit[i]))) + newNum;
    }
    return newNum;
  }
  let overWan = Math.floor(num / 10000);
  let noWan: number | string = num % 10000;
  if (noWan.toString().length < 4) noWan = "0" + noWan;
  return overWan ? getWan(overWan) + "万" + getWan(noWan) : getWan(num);
}

// 节流
export const throttle = (fn: { apply: (arg0: any, arg1: any[]) => void }, t: number) => {
  let flag = true
  const interval = t || 500
  return function (this: any, ...args: any) {
    if (flag) {
      fn.apply(this, args)
      flag = false
      setTimeout(() => {
        flag = true
      }, interval)
    }
  }
}

// 防抖
export const debounce = (fn: { apply: (arg0: any, arg1: any) => void }, t: number) => {
  let timeId: any = null
  const delay = t || 500
  return function (this: any, ...args: any) {
    if (timeId) {
      clearTimeout(timeId)
    }
    timeId = setTimeout(() => {
      timeId = null
      fn.apply(this, args)
    }, delay)
  }
}

/**
 * dateFormat 格式化时间
 * @param {number} time   时间戳
 * @param {string} type   yyyy-MM-dd || yyyy-MM-dd hh:mm:ss
 * @returns  string
 */
export function dateFormat(time: number, type: string): string {
  let formatTime
  let date
  if (time === 0) {
    date = new Date()
  } else {
    date = new Date(time)
  }
  let Year = date.getFullYear()
  let month = date.getMonth() + 1
  let day = date.getDate()
  let Hour = date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()
  let Minute = date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()
  let Second = date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds()
  if (type === 'yyyy-MM-dd') {
    formatTime = Year + '-' + month + '-' + day
    return formatTime
  } else if (type === 'yyyy-MM-dd hh:mm:ss') {
    formatTime = Year + '-' + month + '-' + day + ' ' + Hour + ':' + Minute + ':' + Second
    return formatTime
  } else if (type === 'hh:mm:ss') {
    formatTime = Hour + ':' + Minute + ':' + Second
    return formatTime
  } else {
    return "error type!"
  }
}
